package com.anntly.interceptor;

import cn.hutool.core.util.StrUtil;
import com.anntly.config.DBRouterConfigProperties;
import com.anntly.context.DbContextHolder;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.annotation.Resource;

/**
 * @Author：Anntly
 * @Package：com.anntly.interceptor
 * @Project：dbRouter
 * @name：DynamicDatasource
 * @Date：2024/7/7 16:26
 * @Filename：DynamicDatasource
 */
public class DynamicDatasource extends AbstractRoutingDataSource {

    @Resource
    private DBRouterConfigProperties dbRouterConfigProperties;

    @Override
    protected Object determineCurrentLookupKey() {
        String defaultDb = dbRouterConfigProperties.getDefaultDb();
        if (StrUtil.isNotBlank(DbContextHolder.getDbRouteKey())) {
            return "db" + DbContextHolder.getDbRouteKey();
        } else {
            return defaultDb;
        }
    }
}
